package com.wcy.scl.service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class ComputeController {
	
	private Logger log = LogManager.getLogger(ComputeController.class);
	
	@Autowired
	RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "addFallback")
	@RequestMapping(value="add", method=RequestMethod.GET)
//	public int add(@RequestParam Integer x, @RequestParam Integer y){
	public int add(Integer x, Integer y){
		log.info("ComputeController - call add method, the params x = {}, y = {}", x, y);
		return restTemplate.getForEntity(String.format("http://COMPUTE-SERVICE/add?x=%d&y=%d", x, y), Integer.class).getBody();
	}
	
	public int addFallback(Integer x, Integer y){
		return -1;
	}
}
